<?php

namespace App\Console\Commands;

use App\GatewayWorker\Events;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Illuminate\Console\Command;
use Workerman\Worker;

class GatewayWorkerServer extends Command
{
    // 命令行的名称及签名。
    protected $signature = "gateway-worker:server {action} {--daemon}";
    // 命令行的描述
    protected $description = "Start a GatewayWorker Server.";

    protected $port_9601 = 9601;
    protected $port_9602 = 9602;
    protected $port_start_9000 = 9000;

    public function __construct()
    {
        parent::__construct();
        if(env('APP_SITE_NUM', 0) > 1){
            if(env('APP_SITE_NUM', 0) == 2){
                $this->port_9601 = 10601;
                $this->port_9602 = 10602;
                $this->port_start_9000 = 10000;
            }if(env('APP_SITE_NUM', 0) == 3){
                $this->port_9601 = 11601;
                $this->port_9602 = 11602;
                $this->port_start_9000 = 11000;
            }if(env('APP_SITE_NUM', 0) == 4){
                $this->port_9601 = 12601;
                $this->port_9602 = 12602;
                $this->port_start_9000 = 12000;
            }if(env('APP_SITE_NUM', 0) == 5){
                $this->port_9601 = 13601;
                $this->port_9602 = 13602;
                $this->port_start_9000 = 13000;
            }
        }
    }

    public function handle()
    {
        global $argv;
        if (!in_array($action = $this->argument('action'), ['start', 'stop', 'restart'])) {
            $this->error('Error Arguments');
            exit;
        }
        $argv[0] = 'gateway-worker:server';
        $argv[1] = $action;
        $argv[2] = $this->option('daemon') ? '-d' : '';//该参数是以daemon（守护进程）方式启动
        $this->start();
    }

    public function start()
    {
        $this->startGateWay();
        $this->startBusinessWorker();
        $this->startRegister();
        Worker::runAll();
    }

    public function startBusinessWorker()
    {
        $work = new BusinessWorker();
        $work->name = 'BusinessWorker_'.env('APP_SITE_NUM', 0);#设置BusinessWorker进程的名称
        $work->count = 2;#设置BusinessWorker进程的数量
        $work->registerAddress = '127.0.0.1:'.$this->port_9601;#注册服务地址
        $work->eventHandler = Events::class;#设置使用哪个类来处理业务,业务类至少要实现onMessage静态方法，onConnect和onClose静态方法可以不用实现
    }

    public function startGateWay()
    {
        $gateway = new Gateway("websocket://0.0.0.0:".$this->port_9602);
        $gateway->name = 'Gateway_'.env('APP_SITE_NUM', 0);#设置Gateway进程的名称，方便status命令中查看统计
        $gateway->count = 2;#进程的数量
        $gateway->lanIp = '127.0.0.1';#内网ip,多服务器分布式部署的时候需要填写真实的内网ip
        $gateway->startPort = $this->port_start_9000;#监听本机端口的起始端口
        $gateway->pingInterval = 55;
        $gateway->pingNotResponseLimit = 0;#服务端主动发送心跳
        $gateway->pingData = '{"status":true,"code":200,"mode":2,"tip":"heart","message":"成功","data":[]}';
        $gateway->registerAddress = '127.0.0.1:'.$this->port_9601;#注册服务地址
    }

    public function startRegister()
    {
        new Register('Register_'.env('APP_SITE_NUM', 0),'text://127.0.0.1:'.$this->port_9601);
    }

    //ws = new WebSocket("ws://127.0.0.1:9602");
    //ws.onopen = function() {
    //    ws . send('{"mode":"say","from_uid":1,to_uid:2,"content":"文字内容"}');
    //    ws . send('{"mode":"chats","from_uid":1}');
    //};
    //ws.onmessage = function(e) {
    //    console.log("收到服务端的消息：" + e.data);
    //};
}
